var customerQueryDetail = {
    saleAreas:[],//销售区域
    init: function () {
        if(getQueryString('spuId')){
            var spuId = getQueryString('spuId')
            this.getDetailData(spuId);
        }
        this.addClick()

    },
    addClick: function () {
        // 销售区域操作
        // 销售区域
        $('.saleAreaWarp').delegate('li','click',function(){
            var provinceCode = $(this).attr('data-code');
            var str = '';
            $.each(customerQueryDetail.saleAreas,function(index,item){
                if(provinceCode == item.code){
                    $.each(item.children,function(index,itm){
                        str +='<span>'+itm.name+'、</span>'
                    })
                }
            })
            layer.alert(str,{title:'销售区域',offset:'t'})
        })
    },
    getDetailData: function (spuId) {
        var params = {
            spuId:spuId
        }
        var successData = function (data) {
            if(data.errorCode == 0){
                customerQueryDetail.renderDataOne(data.data)
                customerQueryDetail.renderDataSecond(data.data)
                customerQueryDetail.renderDataThird(data.data.skus)
                customerQueryDetail.renderDataFourth(data.data)
            }else {

            }

        }
        return web.doAjax(root.serviceProviderManagement.getFormalProductDetail, 'post', params, successData, 2);
    },
    // 渲染模块一
    renderDataOne: function (result) {
        var providerName = result.providerName ? result.providerName : '';// 服务商名称
        var customerName = result.channelName ? result.channelName : '';// 专属客户
        var manageRegionName = result.manageRegionName?result.manageRegionName:'';// 管理区域
        var updateTime = result.skus[0].updateTime;// 更新时间
        $('.providerName').html(providerName);//服务商名称
        $('.customerName').html(customerName);//专属渠道
        $('.updateTime').html(updateTime);//专属客户
        $('.manageRegionName').html(manageRegionName)//管理区域
    },
    // 渲染模块二
    renderDataSecond: function (result) {
        var spuName = result.spuName ? result.spuName : '';//商品名称
        var oneCategoryName = result.oneCategoryName ? result.oneCategoryName : '';//一级名称
        var twoCategoryName = result.twoCategoryName ? result.twoCategoryName : '';//二级名称
        var threeCategoryName = result.threeCategoryName ? result.threeCategoryName : '';//三级名称
        var fourCategoryName = result.fourCategoryName ? result.fourCategoryName : '';//四级名称

        var classificationName = result.classificationName ? result.classificationName : '';//归类名称
        var revenueCode = result.revenueCode ? result.revenueCode : '';//税类编码
        var definitionBrandName = result.definitionBrandName;// 自定义品牌名称
        var brandName = result.brandName ? result.brandName : definitionBrandName;//云采品牌
        var discount = result.discount?result.discount *100:'';// 税率
        var origin = result.origin ? result.origin : '';//产地
        var material = result.material ? result.material : '';//材质
        var restricted = '';//限制类型
        if (result.categoryProductType == 1) {
            restricted = '危化品'
        } else if (result.categoryProductType == 2) {
            restricted = '管制刀具'
        } else {
            restricted = '非限制类'
        }
        var spuImages = result.spuImages; //// 渲染图片

        var productQualifications = result.productQualifications;//商品资质
        // 渲染图片
        if (spuImages.length > 0) {
            var str = '';
            spuImages.forEach(function (item) {
                str += '<li class="right" data-src="'+item.attachmentUrl+'" onclick="customerQueryDetail.checkBigPic(this)">' +
                    '<img src="' + item.attachmentUrl + '" alt="" style="width: 60px;height: 60px">' +
                    '  </li>'
            });
            var newStr = '<li class="left">图片：</li>' + str
            $('.picture').html(newStr)
        }
        // 商品资质
        // if(productQualifications){
        //     $('.checkQualification').show()
        //     $('#downLoadQualification').attr('href',root.orderInter.downLoadFromUrl +'?urlStr='+productQualifications+'&fileName=下载pdf模板')
        // }else {
        //     $('.checkQualification').hide()
        // }
        console.log(result);
        if (result.productQualificationAttachments) {
            for (index = 0; index < result.productQualificationAttachments.length; index++) {
                qualificationPDF(result.productQualificationAttachments[index],index);
            }
        }
        var classifyHtml = '<span>' + oneCategoryName + '</span> > <span>' + twoCategoryName + '</span> > <span>' + threeCategoryName + '</span> > <span>'+fourCategoryName+'</span>'   ;
        // 销售区域
        var saleAreas = result.saleAreas?result.saleAreas:[];
        customerQueryDetail.saleAreas = saleAreas;
        if(saleAreas.length >0){
            if (saleAreas[0].code == 0) {
                var strArea = '';
                $.each(saleAreas,function(index,item){
                    strArea +=' <li style="cursor: pointer;display: inline-block;margin-right: 7px" data-code="'+item.code+'">'+item.name+''+'</li>'
                });
            } else {
                var strArea = '';
                $.each(saleAreas,function(index,item){
                    strArea +=' <li style="cursor: pointer;display: inline-block;margin-right: 7px" data-code="'+item.code+'">'+item.name+'' +
                        ' <span style="color: #0a60d6">('+item.children.length+')</span>' +
                        '</li>'
                });
            }
            $('.saleAreaWarp').html(strArea);
        }
        $('.defaultClassify').html(classifyHtml)
        /* 模块二数据回显 start */
        $('.spuName').html(spuName);
        $('.classificationName').html(classificationName);
        $('.revenueCode').html(revenueCode);
        $('.brandName').html(brandName);
        $('.origin').html(origin);
        $('.material').html(material);
        $('.restricted').html(restricted);
        $('.discount').html('<span>'+discount+'%</span>')

    },
    // 渲染模块三
    renderDataThird: function (result) {
        var dataSource = [];
        $.each(result, function (index, item) {
            var obj = {};
            obj.specifications = item.specifications;// 规格
            obj.color = item.color; //颜色
            obj.channelPrice = item.channelPrice;// 售价
            obj.unitName = item.unitName;// 单位
            obj.xfsPurchasePrice = item.xfsPurchasePrice; // 鑫方盛采购价
            obj.deliveryDay = item.deliveryDay; // 预计出货日
            obj.miniOrder = item.miniOrder; // 最小起订量
            obj.virtualStock = item.virtualStock; // 虚拟库存
            obj.maxLength = item.maxLength;//最长边
            obj.volume = item.volume;//体积
            obj.weight = item.weight;//重量
            dataSource.push(obj)
        });
        customerQueryDetail.tableLists(dataSource)
    },
    tableLists: function (data) {
        var dataTable = $('#datatable1').dataTable();
        dataTable.fnClearTable();
        dataTable.fnDestroy();
        $("#datatable1").DataTable({
            "order": [[1, "desc"]],
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                }
            },
            data: data,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var specifications = row.specifications;
                        if (!specifications) {
                            specifications = ''
                        }
                        return '<span style="text-align: center;" >' + specifications + '</span>';
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var color = row.color;
                        if (!color) {
                            color = ''
                        }
                        return '<span style="text-align: center;" >' + color + '</span>';

                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var channelPrice = row.channelPrice;
                        if (!channelPrice) {
                            channelPrice = ''
                        }

                        return '<span style="text-align: center;">¥' + channelPrice.toFixed(2) + '/' + row.unitName + '</span>';
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var xfsPurchasePrice = row.xfsPurchasePrice;
                        if (!xfsPurchasePrice) {
                            xfsPurchasePrice = '';
                        }
                        return '<span style="text-align: center;">¥' + xfsPurchasePrice.toFixed(2) + '/' + row.unitName + '</span>';
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var deliveryDay = row.deliveryDay;
                        if (!deliveryDay) {
                            deliveryDay = '';
                        }
                        return '<span style="text-align: center;">' + deliveryDay + ' 天</span>';
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var miniOrder = row.miniOrder;
                        if (!miniOrder) {
                            miniOrder = '';
                        }
                        return '<span style="text-align: center;">' + miniOrder + ' '+ row.unitName+'</span>';
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var virtualStock = row.virtualStock;
                        if (!virtualStock) {
                            virtualStock = '';
                        }
                        return '<span style="text-align: center;">' + virtualStock + '</span>';
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                        var maxLength = row.maxLength?row.maxLength:'';
                        return '<span style="text-align: center;">' + maxLength + 'cm</span>';
                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row, meta) {
                        var volume = row.volume?row.volume:'';
                        return '<span style="text-align: center;">' + volume + 'm³</span>';
                    },
                    "targets": 8
                },
                {
                    "render": function (data, type, row, meta) {
                        var weight = row.weight?row.weight:'';
                        return '<span style="text-align: center;">' + weight + 'kg</span>';
                    },
                    "targets": 9
                },
            ],
        })
    },
    // 渲染模块四
    renderDataFourth: function (result) {
        $('.detail').html(result.detail)
    },
    // 点击查看大图
    checkBigPic:function(imgSrc){
        var imgUrl = $(imgSrc).attr('data-src')
        var title = '查看图片';
        var url = '../../goods/picShow.html';
        window.event ? window.event.cancelBubble = true : e.stopPropagation();
        sessionStorage.setItem("imgUrl", imgUrl);
        layer_show(title, url, "800", "500");
    }

}
customerQueryDetail.init();



//资质pdf渲染
function qualificationPDF(pdf,index)
{
    var qualification_Html = "<div class=\"qualification_module\">";
        qualification_Html += "<img class=\"qualification-img\" src=\"../../../img/Pdf.png\">";
        qualification_Html += "<div class=\"qualification_oper\">";
        qualification_Html += "<a href=\"" + root.orderInter.downLoadFromUrl + "?urlStr="+ pdf.attachmentUrl + "&fileName=下载pdf模板\" class=\"downLoadQualification\">点击下载附件查看</a>";
        qualification_Html += "</div>";
        qualification_Html += "</div>";
        $("#qualification_list").prepend(qualification_Html);
}